/*
 * Project Name: cms_web
 * File Name: RoleAction.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年3月9日 下午3:25:31
 */
package com.shishuo.cms.action.manage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.UserMenuEntity;
import com.shishuo.cms.entity.UserRoleEntity;
import com.shishuo.cms.entity.UserRoleMenuEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.UserMenuService;
import com.shishuo.cms.service.UserRoleMenuService;
import com.shishuo.cms.service.UserRoleService;

@Controller
@RequestMapping("/admin/userRole")
public class UserRoleAction extends AdminBaseAction
{
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private UserRoleMenuService userRoleMenuService;
	@Autowired
	private UserMenuService userMenuService;
	
	@InitBinder("userRoleEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("userRoleEntity.");
	}
	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String addRole(ModelMap modelMap) {
		return "system/userRole/add";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addNewRole(@ModelAttribute UserRoleEntity userRoleEntity) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (userRoleEntity==null||userRoleEntity.getRoleName()==null||userRoleEntity.getRoleName().equals(""))
		{
			json.getErrors().put("roleName", "角色名称不能为空");
		}
		if(validateJsonIsError(json))return json;
		userRoleService.insert(userRoleEntity);
		json.setResult(true);
		return json;
	}
	
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String RolePage(@RequestParam(value = "p", defaultValue = "1") int pageNum,ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", userRoleService.getAllListPage(pageNum));
		return "system/userRole/all";
	}
	
	
	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(
							@RequestParam(value = "id", defaultValue = "0") long id,
							ModelMap modelMap, HttpServletRequest request) throws Exception
	{
		UserRoleEntity roleEntity = userRoleService.getById(id);
		modelMap.put("roleEntity", roleEntity);
		return "system/userRole/update";
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> updateRole(@ModelAttribute UserRoleEntity roleEntity) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (roleEntity==null||roleEntity.getId()==0||roleEntity.getRoleName().equals(""))
		{
			json.getErrors().put("roleName", "角色名称不能为空");
		}
		if(validateJsonIsError(json))return json;
		userRoleService.update(roleEntity);
		json.setResult(true);
		return json;
	}
	
	
	@AuthPassport
	@RequestMapping(value = "/config_role_menu.htm", method = RequestMethod.GET)
	public String configRoleMenu(@RequestParam(value = "roleId", defaultValue = "0") long roleId,ModelMap modelMap, HttpServletRequest request) throws Exception
	{
		List<UserMenuEntity> allMenus=userMenuService.getAllList();
		modelMap.put("allMenus", allMenus);
		
		List<UserRoleMenuEntity> selectRoleMenus=userRoleMenuService.getListByRoleId(roleId);
		modelMap.put("selectRoleMenus", selectRoleMenus);
		modelMap.put("roleId", roleId);
		return "system/userRole/config_role_menu";
	}
	
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/configRoleMenu.json", method = RequestMethod.POST)
	public JsonVo<String> configRoleMenu(@RequestParam(value = "selectMenuIds",required=false) long[] selectMenuIds,@RequestParam(value = "roleId", defaultValue = "0") long roleId) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (roleId==0)
		{
			json.getErrors().put("roleName", "角色不存在");
		}
		if(validateJsonIsError(json))return json;
		userRoleMenuService.configRoleMenu(selectMenuIds,roleId);
		json.setResult(true);
		return json;
	}
	
	
	
}
